<template>
  <div>
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="公司名称"
        style="width: 100px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.importance"
        placeholder="公司状态"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in importanceOptions"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>

      <el-select
        v-model="listQuery.sort"
        style="width: 140px"
        class="filter-item"
      >
        <el-option
          v-for="item in sortOptions"
          :key="item.key"
          :label="item.label"
          :value="item.key"
        />
      </el-select>

      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>

      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        新增公司
      </el-button>

      <el-button
        v-waves
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="handleDownload"
      >
        导出
      </el-button>

      <el-button
        v-waves
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-upload2"
        @click="handleDownload"
      >
        导入
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="60">
        <template slot-scope="scope">
          <span>{{ scope.row.adminId }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="公司名称"
        prop="name"
        sortable="custom"
        align="center"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.adminName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="商家账号"
        prop="password"
        sortable="custom"
        align="center"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="商家地址"
        prop="role"
        sortable="custom"
        align="center"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.address }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="商家电话"
        prop="role"
        sortable="custom"
        align="center"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.adminPhone }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="商家创建时间"
        prop="role"
        sortable="custom"
        align="center"
        width="155"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="最后修改时间"
        prop="role"
        sortable="custom"
        align="center"
        width="155"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.modifyTime }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="商家状态"
        prop="role"
        sortable="custom"
        align="center"
        width="110"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.status === 1 ? "运营良好":"濒临破产" }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width="180"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row}">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(row)"
          >
            修改
          </el-button>
          <el-button
            v-if="row.status!=='deleted'"
            size="mini"
            type="danger"
            @click="handleDel(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      small
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="110px"
        style="width: 400px;
               margin-left:50px;"
      >

        <el-form-item label="公司名称" prop="adminName">
          <el-input v-model="temp.adminName" />
        </el-form-item>

        <el-form-item label="商家账号" prop="username">
          <el-input v-model="temp.username" />
        </el-form-item>

        <el-form-item label="商家密码" prop="password">
          <el-input v-model="temp.password" />
        </el-form-item>

        <el-form-item label="商家地址" prop="address">
          <el-input v-model="temp.address" />
        </el-form-item>

        <el-form-item label="商家电话" prop="adminPhone">
          <el-input v-model="temp.adminPhone" />
        </el-form-item>

        <el-form-item label="商家创建时间" prop="createTime">
          <el-input v-model="temp.createTime" type="date" />
        </el-form-item>

        <el-form-item label="最后修改时间" prop="modifyTime">
          <el-input v-model="temp.modifyTime" type="date" />
        </el-form-item>

        <el-form-item label="商家状态" prop="status">
          <el-input v-model="temp.status" />
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import ajax from '@/utils/baseurl'
import Pagination from '@/components/Pagination'

var p
export default {
  name: 'Administrators',
  directive: { waves },
  components: { Pagination },
  data() {
    return {
      temp: {
        adminName: '',
        username: '',
        password: '',
        address: '',
        adminPhone: '',
        createTime: '',
        modifyTime: '',
        status: ''
      },
      rules: {
        // username: [{required: true, message: '用户不能为空!', trigger: 'change'}],
        // password: [{required: true, message: '登录密码不能为空!', trigger: 'change'}]
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      listQuery: {
        page: 1,
        limit: 10,
        importance: '',
        title: '',
        sort: '+id'
      },
      importanceOptions: ['运营良好', '濒临破产'],
      sortOptions: [{ label: 'ID 升序', key: '+id' },
        { label: 'ID 降序', key: '-id' }],
      total: 0,
      tableKey: 0,
      listLoading: true,
      list: null
    }
  },
  created() {
    p = this
    this.getList()
  },
  methods: {
    updateData() {
      this.createData()
    },
    resetTemp() {
      this.temp = {
        adminName: '',
        username: '',
        password: '',
        address: '',
        adminPhone: '',
        createTime: '',
        modifyTime: '',
        status: ''
      }
    },
    createData() {
      var p = this
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          ajax(p.dialogStatus === 'create' ? 'addAdmin.html' : 'updateAdmin.html', this.temp, function(data) {
            if (data) {
              p.dialogFormVisible = false
              p.getList()
              p.$notify({
                title: '搞定',
                message: p.dialogStatus === 'create' ? '添加成功' : '修改成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDel(row) {
      var p = this
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ajax('delAdminById.html', { 'id': row.adminId }, function(data) {
          if (data) {
            p.getList()
            p.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleFilter() {
      this.listLoading = true
      ajax('getAdminByPage.html', p.listQuery, function(data) {
        data = JSON.parse(data)
        p.list = data.page.list
        p.total = data.page.totalCount
        p.listLoading = false
      })
    },
    getList() {
      this.listLoading = true
      ajax('getAdminByPage.html', p.listQuery, function(data) {
        data = JSON.parse(data)
        p.list = data.page.list
        p.total = data.page.totalCount
        p.listLoading = false
      })
    }
  }
}
</script>
